package com.ym.jiujiangeyou;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.map.ItemizedOverlay;
import com.baidu.mapapi.map.LocationData;
import com.baidu.mapapi.map.MKEvent;
import com.baidu.mapapi.map.MKMapViewListener;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationOverlay;
import com.baidu.mapapi.map.OverlayItem;
import com.baidu.mapapi.map.PoiOverlay;
import com.baidu.mapapi.map.PopupClickListener;
import com.baidu.mapapi.map.PopupOverlay;
import com.baidu.mapapi.map.MyLocationOverlay.LocationMode;
import com.baidu.mapapi.search.MKAddrInfo;
import com.baidu.mapapi.search.MKBusLineResult;
import com.baidu.mapapi.search.MKDrivingRouteResult;
import com.baidu.mapapi.search.MKPoiInfo;
import com.baidu.mapapi.search.MKPoiResult;
import com.baidu.mapapi.search.MKSearch;
import com.baidu.mapapi.search.MKSearchListener;
import com.baidu.mapapi.search.MKShareUrlResult;
import com.baidu.mapapi.search.MKSuggestionResult;
import com.baidu.mapapi.search.MKTransitRouteResult;
import com.baidu.mapapi.search.MKWalkingRouteResult;
import com.baidu.navisdk.BNaviEngineManager.NaviEngineInitListener;
import com.baidu.navisdk.BaiduNaviManager;
import com.baidu.navisdk.BaiduNaviManager.OnStartNavigationListener;
import com.baidu.navisdk.comapi.routeplan.RoutePlanParams.NE_RoutePlan_Mode;
import com.baidu.navisdk.util.verify.BNKeyVerifyListener;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.ym.jiujiangeyou.app.AppManager;
import com.ym.jiujiangeyou.app.MyApplication;
import com.ym.jiujiangeyou.ui.DialogView;
import com.ym.jiujiangeyou.ui.MyPoiOverlay;
import com.ym.jiujiangeyou.util.Constants;
import com.ym.jiujiangeyou.util.MapBitmapUtil;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author 叶铭
 * @email yeming_1001@163.com
 * @version V1.0
 */
public class SceneryMapActivity extends Activity implements OnClickListener {

	/**
	 * 用MapController完成地图控制
	 */
	private MapController mMapController = null;
	/**
	 * MKMapViewListener 用于处理地图事件回调
	 */
	MKMapViewListener mMapListener = null;
	private TextView title_text;
	String locAddress;
	GeoPoint p;
	ImageView to_pointer, scenery_pointer, scenery_poi_pointer,
			hotel_poi_pointer, food_poi_pointer;
	LinearLayout location_pointer, sight_pointer, route_plan_pointer,
			navi_pointer;

	// 定位相关
	LocationClient mLocClient;
	LocationData locData = null;
	public MyLocationListenner myListener = new MyLocationListenner();
	boolean isRequest = false;// 是否手动触发请求定位
	boolean isFirstLoc = true;// 是否首次定位
	// 定位图层
	locationOverlay myLocationOverlay = null;
	// MyLocationMapView mMapView = null; // 地图View
	private MapView mMapView = null;
	MyApplication app = null;
	// 弹出泡泡图层
	private PopupOverlay pop = null;// 弹出泡泡图层，浏览节点时使用
	// private PopupOverlay spop = null;// 弹出泡泡图层，浏览节点时使用
	private TextView popupText = null;// 泡泡view
	private View viewCache = null;
	double cLat = 29.6147274785;// 39.945 庐山经纬度
	double cLon = 115.8862775142;// 116.404
	// double hLat = 29.6147274785;// 39.945 庐山经纬度
	// double hLon = 115.8862775142;// 116.404
	String name = null;
	String flag = null;

	private MKSearch mSearch = null; // 搜索模块
	MKSearchListener searchListener = null;

	Dialog dialog, dialog1;
	Toast toast;
	// 导航相关
	private boolean mIsEngineInitSuccess = false;

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case Constants.MAP_LOAD_FINISH:
				dialog.dismiss();
				toast = DialogView.popToast(SceneryMapActivity.this,
						R.string.map_load_finish);
				toast.show();
				break;
			case Constants.MAP_LOCATION_FINISH:
				dialog1.dismiss();
			default:
				break;
			}

		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		app = (MyApplication) this.getApplication();
		// 添加Activity到堆栈
		AppManager.getAppManager().addActivity(this);
		if (app.mBMapManager == null) {
			app.mBMapManager = new BMapManager(getApplicationContext());
			/**
			 * 如果BMapManager没有初始化则初始化BMapManager
			 */
			app.mBMapManager.init(new MyApplication.MyGeneralListener());
		}
		setContentView(R.layout.scenery_map);

		// 初始化导航引擎
		BaiduNaviManager.getInstance().initEngine(this, getSdcardDir(),
				mNaviEngineInitListener, Constants.ACCESS_KEY,
				mKeyVerifyListener);

		title_text = (TextView) this.findViewById(R.id.title_text);
		title_text.setText(R.string.title_map_text);
		// to_pointer = (ImageView) this.findViewById(R.id.to_pointer);
		// scenery_pointer = (ImageView)
		// this.findViewById(R.id.scenery_pointer);
		scenery_poi_pointer = (ImageView) this
				.findViewById(R.id.scenery_poi_pointer);
		hotel_poi_pointer = (ImageView) this
				.findViewById(R.id.hotel_poi_pointer);
		food_poi_pointer = (ImageView) this.findViewById(R.id.food_poi_pointer);
		location_pointer = (LinearLayout) this
				.findViewById(R.id.location_pointer);
		sight_pointer = (LinearLayout) this.findViewById(R.id.sight_pointer);
		route_plan_pointer = (LinearLayout) this
				.findViewById(R.id.route_plan_pointer);
		navi_pointer = (LinearLayout) this.findViewById(R.id.navi_pointer);

		// to_pointer.setOnClickListener(this);
		// scenery_pointer.setOnClickListener(this);
		scenery_poi_pointer.setOnClickListener(this);
		hotel_poi_pointer.setOnClickListener(this);
		food_poi_pointer.setOnClickListener(this);
		location_pointer.setOnClickListener(this);
		sight_pointer.setOnClickListener(this);
		route_plan_pointer.setOnClickListener(this);
		navi_pointer.setOnClickListener(this);

		/*
		 * scenery_pointer.setOnClickListener(new OnClickListener() {
		 * 
		 * @Override public void onClick(View v) { // TODO Auto-generated method
		 * stub mMapController.setZoom(13); mMapView.refresh();
		 * mMapController.setCenter(p);
		 * 
		 * } });
		 */

		mMapView = (MapView) findViewById(R.id.smapView);
		/**
		 * 获取地图控制器
		 */
		mMapController = mMapView.getController();
		/**
		 * 设置地图是否响应点击事件 .
		 */
		mMapController.enableClick(true);
		/**
		 * 设置地图缩放级别
		 */
		mMapController.setZoom(13);
		// double cLat = 29.6147274785;//39.945 庐山经纬度
		// double cLon = 115.8862775142;//116.404
		Intent intent = getIntent();
		if (intent.hasExtra("sLon") && intent.hasExtra("sLat")
				|| intent.hasExtra("hLon") && intent.hasExtra("hLat")) {
			// 当用intent参数时，设置中心点为指定点
			Bundle b = intent.getExtras();
			/*
			 * sName = b.getString("sName"); cLat = b.getDouble("sLat"); cLon =
			 * b.getDouble("sLon"); Constants.NAVI_END_LAT = cLat ;
			 * Constants.NAVI_END_LON = cLon ; Constants.NAVI_END_NAME = sName ;
			 */
			flag = b.getString("flag");
			if (flag.equals("hotel")) {
				name = b.getString("hName");
				cLat = b.getDouble("hLat");
				cLon = b.getDouble("hLon");
				Constants.NAVI_END_NAME = name;
			} else if (flag.equals("scenery")) {
				name = b.getString("sName");
				cLat = b.getDouble("sLat");
				cLon = b.getDouble("sLon");
				Constants.NAVI_END_NAME = name;
			}
			Constants.NAVI_END_LAT = cLat;
			Constants.NAVI_END_LON = cLon;
			// System.out.println("-------------------"+flag+"///"+cLat);
			p = new GeoPoint((int) (cLat * 1E6), (int) (cLon * 1E6));
		} else {
			// 设置中心点为庐山
			p = new GeoPoint((int) (cLat * 1E6), (int) (cLon * 1E6));
		}
		mMapController.setCenter(p);

		/*
		 * Drawable mark = getResources().getDrawable(R.drawable.sm_mark_icon);
		 * OverlayItem item = new OverlayItem(p, "item", "item");
		 * item.setMarker(mark); OverlayTest itemOverlay = new OverlayTest(mark,
		 * mMapView);
		 */
		mMapView.getOverlays().clear();
		mMapView.getOverlays().add(getItemOverlay());
		/* itemOverlay.addItem(item); */
		mMapView.refresh();
		// showMyPopup() ;

		/**
		 * MapView的生命周期与Activity同步，当activity挂起时需调用MapView.onPause()
		 */
		mMapListener = new MKMapViewListener() {
			@Override
			public void onMapMoveFinish() {
				/**
				 * 在此处理地图移动完成回调 缩放，平移等操作完成后，此回调被触发
				 */
			}

			@Override
			public void onClickMapPoi(MapPoi mapPoiInfo) {
				/**
				 * 在此处理底图poi点击事件 显示底图poi名称并移动至该点 设置过：
				 * mMapController.enableClick(true); 时，此回调才能被触发
				 * 
				 */
				String title = "";
				if (mapPoiInfo != null) {
					title = mapPoiInfo.strText;
					Toast.makeText(SceneryMapActivity.this, title,
							Toast.LENGTH_SHORT).show();
					mMapController.animateTo(mapPoiInfo.geoPt);
				}
			}

			@Override
			public void onGetCurrentMap(Bitmap b) {
				/**
				 * 当调用过 mMapView.getCurrentMap()后，此回调会被触发 可在此保存截图至存储设备
				 */
			}

			@Override
			public void onMapAnimationFinish() {
				/**
				 * 地图完成带动画的操作（如: animationTo()）后，此回调被触发
				 */
			}

			/**
			 * 在此处理地图载完成事件
			 */
			@Override
			public void onMapLoadFinish() {
				/*
				 * Toast.makeText(SceneryMapActivity.this, "地图加载完成",
				 * Toast.LENGTH_SHORT).show();
				 */
				Message message = new Message();
				message.what = Constants.MAP_LOAD_FINISH;
				handler.sendMessage(message);
			}
		};
		mMapView.regMapViewListener(MyApplication.getInstance().mBMapManager,
				mMapListener);
		// 注册poi搜索事件监听
		searchListener = new MKSearchListener() {

			@Override
			public void onGetWalkingRouteResult(MKWalkingRouteResult arg0,
					int arg1) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onGetTransitRouteResult(MKTransitRouteResult arg0,
					int arg1) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onGetShareUrlResult(MKShareUrlResult arg0, int arg1,
					int arg2) {
				// TODO Auto-generated method stub

			}

			// 在此处理poi搜索结果
			@Override
			public void onGetPoiResult(MKPoiResult res, int type, int error) {
				// TODO Auto-generated method stub
				if (error == MKEvent.ERROR_RESULT_NOT_FOUND) {
					Toast.makeText(SceneryMapActivity.this, "抱歉，未找到结果",
							Toast.LENGTH_LONG).show();
					return;
				} else if (error != 0 || res == null) {
					Toast.makeText(SceneryMapActivity.this, "搜索出错啦..",
							Toast.LENGTH_LONG).show();
					return;
				}
				// 将poi结果显示到地图上
				MyPoiOverlay poiOverlay = new MyPoiOverlay(
						SceneryMapActivity.this, mMapView, mSearch);
				poiOverlay.setData(res.getAllPoi());
				mMapView.getOverlays().clear();
				// mMapView.getOverlay().clear() ;
				mMapController.setZoom(13);
				mMapView.getOverlays().add(getItemOverlay());
				mMapView.getOverlays().add(poiOverlay);
				mMapView.refresh();
				// 当ePoiType为2（公交线路）或4（地铁线路）时， poi坐标为空
				for (MKPoiInfo info : res.getAllPoi()) {
					if (info.pt != null) {
						mMapView.getController().animateTo(info.pt);
						break;
					}
				}
			}

			@Override
			public void onGetPoiDetailSearchResult(int type, int error) {
				// TODO Auto-generated method stub
				if (error != 0) {
					Toast.makeText(SceneryMapActivity.this, "抱歉，未找到结果",
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(SceneryMapActivity.this, "成功，查看详情页面",
							Toast.LENGTH_SHORT).show();
				}
			}

			@Override
			public void onGetDrivingRouteResult(MKDrivingRouteResult arg0,
					int arg1) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
				// TODO Auto-generated method stub

			}
		};

		dialog = DialogView.popDialog(this, R.string.isloading);
		dialog.show();

	}

	public OverlayTest getItemOverlay() {
		Drawable mark = getResources().getDrawable(R.drawable.sm_mark_icon);
		OverlayItem item = new OverlayItem(p, "item", "item");
		item.setMarker(mark);
		OverlayTest itemOverlay = new OverlayTest(mark, mMapView);
		itemOverlay.addItem(item);
		return itemOverlay;
	}

	/**
	 * 手动触发一次定位请求
	 */
	public void requestLocClick() {
		isRequest = true;
		mLocClient.requestLocation();
		Toast.makeText(SceneryMapActivity.this, "正在定位……", Toast.LENGTH_SHORT)
				.show();
	}

	/**
	 * 定位SDK监听函数
	 */
	public class MyLocationListenner implements BDLocationListener {

		@Override
		public void onReceiveLocation(BDLocation location) {
			if (location == null)
				return;

			locData.latitude = location.getLatitude();
			locData.longitude = location.getLongitude();
			// 如果不显示定位精度圈，将accuracy赋值为0即可
			locData.accuracy = location.getRadius();
			// 此处可以设置 locData的方向信息, 如果定位 SDK 未返回方向信息，用户可以自己实现罗盘功能添加方向信息。
			locData.direction = location.getDerect();
			locAddress = location.getAddrStr();
			System.out.println("-------------------" + location.getAddrStr());
			System.out.println("-------------------" + location.getLatitude());
			System.out.println("-------------------" + location.getLongitude());
			Constants.NAVI_START_LAT = location.getLatitude();
			Constants.NAVI_START_LON = location.getLongitude();
			Constants.NAVI_START_NAME = location.getAddrStr();

			// 更新定位数据
			myLocationOverlay.setData(locData);
			// mMapView.getOverlays().add(myLocationOverlay);
			// 更新图层数据执行刷新后生效
			mMapView.refresh();
			// 是手动触发请求或首次定位时，移动到定位点
			if (isRequest || isFirstLoc) {
				// 移动地图到定位点
				Log.d("LocationOverlay", "receive location, animate to it");
				mMapController.animateTo(new GeoPoint(
						(int) (locData.latitude * 1e6),
						(int) (locData.longitude * 1e6)));
				isRequest = false;
				// myLocationOverlay.setLocationMode(LocationMode.FOLLOWING);
				// System.out.println("--------2-------"+locData.latitude+"----"+locData.longitude
				// );
			}
			// 首次定位完成
			isFirstLoc = false;
			Message message = new Message();
			message.what = Constants.MAP_LOCATION_FINISH;
			handler.sendMessage(message);
		}

		public void onReceivePoi(BDLocation poiLocation) {
			if (poiLocation == null) {
				return;
			}
		}
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		mMapView.onSaveInstanceState(outState);
		super.onSaveInstanceState(outState);
		// getSupportFragmentManager().putFragment(outState, "mContent",
		// mContent) ;
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		mMapView.onResume();
		super.onResume();

	}

	@Override
	public void onPause() {
		mMapView.onPause();
		super.onPause();
	}

	@Override
	public void onStop() {
		super.onStop();
	}

	@Override
	public void onDestroy() {
		mMapView.destroy();
		if (mSearch != null) {
			mSearch.destory();
		}
		// 退出应用调用BMapManager的destroy()方法
		if (app.mBMapManager != null) {
			app.mBMapManager.destroy();
			app.mBMapManager = null;
		}
		// 退出时销毁定位
		if (mLocClient != null) {
			mLocClient.stop();
		}
		super.onDestroy();
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		mMapView.onRestoreInstanceState(savedInstanceState);
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		// overridePendingTransition(R.anim.out_to_left, R.anim.in_from_right) ;
		// this.finish() ;
	}

	/**
	 * 创建弹出泡泡图层
	 */
	public void createPaopao() {
		viewCache = getLayoutInflater()
				.inflate(R.layout.custom_text_view, null);
		popupText = (TextView) viewCache.findViewById(R.id.textcache);
		// 泡泡点击响应回调
		PopupClickListener popListener = new PopupClickListener() {
			@Override
			public void onClickedPopup(int index) {
				Log.v("click", "clickapoapo");
			}
		};
		pop = new PopupOverlay(mMapView, popListener);
		MyLocationMapView.pop = pop;
	}

	/**
	 * 修改位置图标
	 * 
	 * @param marker
	 */
	public void modifyLocationOverlayIcon(Drawable marker) {
		// 当传入marker为null时，使用默认图标绘制
		myLocationOverlay.setMarker(marker);
		// 修改图层，需要刷新MapView生效
		mMapView.refresh();
	}

	class OverlayTest extends ItemizedOverlay<OverlayItem> {
		// 用MapView构造ItemizedOverlay
		public OverlayTest(Drawable mark, MapView mapView) {
			super(mark, mapView);
		}

		protected boolean onTap(int index) {
			// 在此处理item点击事件
			// mMapController.setCenter(p);
			/*
			 * if (sName != null) { Toast.makeText(getApplicationContext(),
			 * sName, Toast.LENGTH_SHORT).show(); }
			 */
			return true;
		}

		public boolean onTap(GeoPoint pt, MapView mapView) {
			// 在此处理MapView的点击事件，当返回 true时
			super.onTap(pt, mapView);
			/*
			 * View view = getLayoutInflater().inflate(R.layout.pop, null) ;
			 * TextView text = (TextView) view.findViewById(R.id.textView_pop) ;
			 * //text.setBackgroundResource(R.drawable.) spop.showPopup(view,
			 * pt, 32) ;
			 */
			return false;
		}
	}

	// 继承MyLocationOverlay重写dispatchTap实现点击处理
	public class locationOverlay extends MyLocationOverlay {

		public locationOverlay(MapView mapView) {
			super(mapView);
			// TODO Auto-generated constructor stub
		}

		@Override
		protected boolean dispatchTap() {
			// TODO Auto-generated method stub
			// 处理点击事件,弹出泡泡
			if (locAddress != null) {
				popupText.setText(locAddress);
			} else {
				popupText.setText("我的位置");
			}
			popupText.setBackgroundResource(R.drawable.poptest);
			pop.showPopup(MapBitmapUtil.getBitmapFromView(popupText),
					new GeoPoint((int) (locData.latitude * 1e6),
							(int) (locData.longitude * 1e6)), 8);
			return true;
		}

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.location_pointer:
			if (isFirstLoc) {
				dialog1 = DialogView.popDialog(SceneryMapActivity.this,
						R.string.map_location_loading);
				dialog1.show();
				// 创建 弹出泡泡图层
				createPaopao();
				// 定位初始化
				mLocClient = new LocationClient(getApplicationContext());
				locData = new LocationData();
				mLocClient.registerLocationListener(myListener);
				LocationClientOption option = new LocationClientOption();
				option.setOpenGps(true);// 打开gps
				option.setCoorType("bd09ll"); // 设置坐标类型
				option.setAddrType("all");// 返回的定位结果包含地址信息
				option.disableCache(false);// 禁止启用缓存定位
				// option.setPoiNumber(5);
				// option.setPoiDistance(1000); //poi查询距离
				// option.setPoiExtraInfo(true); //是否需要POI的电话和地址等详细信息
				// option.setScanSpan(10000);
				mLocClient.setLocOption(option);
				// mLocClient.requestLocation();
				// mLocClient.requestPoi();
				mLocClient.start();

				// System.out.println("----------1-----------------") ;
				// 定位图层初始化
				myLocationOverlay = new locationOverlay(mMapView);
				// 设置定位数据
				myLocationOverlay.setData(locData);
				// 添加定位图层
				mMapView.getOverlays().add(myLocationOverlay);
				myLocationOverlay.setLocationMode(LocationMode.NORMAL);// 设置定位模式)
				myLocationOverlay.enableCompass();
				mMapController.setZoom(17);
				// 修改定位数据后刷新图层生效
				mMapView.refresh();
			} else {
				requestLocClick();
				mMapView.getOverlays().clear();
				mMapController.setZoom(17);
				mMapView.getOverlays().add(myLocationOverlay);
				// modifyLocationOverlayIcon(null) ;
				mMapView.refresh();
			}
			break;
		case R.id.sight_pointer:
			mMapView.getOverlays().clear();
			mMapView.getOverlays().add(getItemOverlay());
			mMapController.setZoom(13);
			mMapView.refresh();
			mMapController.setCenter(p);
			break;
		case R.id.scenery_poi_pointer:
			System.out.println("----------1--------------");
			mSearch = new MKSearch();
			mSearch.init(app.mBMapManager, searchListener);
			mSearch.poiSearchNearBy("景点", new GeoPoint((int) (cLat * 1E6),
					(int) (cLon * 1E6)), 5000);
			break;
		case R.id.food_poi_pointer:
			System.out.println("----------2--------------");
			mSearch = new MKSearch();
			mSearch.init(app.mBMapManager, searchListener);
			mSearch.poiSearchNearBy("美食", new GeoPoint((int) (cLat * 1E6),
					(int) (cLon * 1E6)), 5000);
			break;
		case R.id.hotel_poi_pointer:
			System.out.println("----------3--------------");
			mSearch = new MKSearch();
			mSearch.init(app.mBMapManager, searchListener);
			mSearch.poiSearchNearBy("住宿", new GeoPoint((int) (cLat * 1E6),
					(int) (cLon * 1E6)), 5000);
			break;
		case R.id.route_plan_pointer:
			Intent intent = new Intent(SceneryMapActivity.this,
					RoutePlanActivity.class);
			Bundle bundle = new Bundle();
			bundle.putDouble("cLat", cLat);// scenery.getLatitude()
			bundle.putDouble("cLon", cLon);// scenery.getLongitude()
			bundle.putString("name", name);
			intent.putExtras(bundle);
			startActivity(intent);
			break;
		case R.id.navi_pointer:
			if (Constants.NAVI_START_NAME == null
					|| Constants.NAVI_START_LAT == 0
					|| Constants.NAVI_START_LON == 0) {
				Toast.makeText(SceneryMapActivity.this, "请先定位...",
						Toast.LENGTH_SHORT).show();
			} else {
				launchNavigator();
			}
			break;
		default:
			break;
		}
	}

	// 导航相关
	private String getSdcardDir() {
		if (Environment.getExternalStorageState().equalsIgnoreCase(
				Environment.MEDIA_MOUNTED)) {
			return Environment.getExternalStorageDirectory().toString();
		}
		return null;
	}

	private NaviEngineInitListener mNaviEngineInitListener = new NaviEngineInitListener() {
		public void engineInitSuccess() {
			mIsEngineInitSuccess = true;
		}

		public void engineInitStart() {
		}

		public void engineInitFail() {
		}
	};
	private BNKeyVerifyListener mKeyVerifyListener = new BNKeyVerifyListener() {

		@Override
		public void onVerifySucc() {
			// TODO Auto-generated method stub
			Toast.makeText(SceneryMapActivity.this, "key校验成功",
					Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onVerifyFailed(int arg0, String arg1) {
			// TODO Auto-generated method stub
			Toast.makeText(SceneryMapActivity.this, "key校验失败",
					Toast.LENGTH_SHORT).show();
		}
	};

	/**
	 * 启动GPS导航. 前置条件：导航引擎初始化成功
	 */
	private void launchNavigator() {
		// 这里给出一个起终点示例，实际应用中可以通过POI检索、外部POI来源等方式获取起终点坐标
		BaiduNaviManager.getInstance().launchNavigator(this,
				Constants.NAVI_START_LAT, Constants.NAVI_START_LON,
				Constants.NAVI_START_NAME, Constants.NAVI_END_LAT,
				Constants.NAVI_END_LON, Constants.NAVI_END_NAME,
				NE_RoutePlan_Mode.ROUTE_PLAN_MOD_MIN_TIME, // 算路方式
				true, // 真实导航
				BaiduNaviManager.STRATEGY_FORCE_ONLINE_PRIORITY, // 在离线策略
				new OnStartNavigationListener() { // 跳转监听

					@Override
					public void onJumpToNavigator(Bundle configParams) {
						Intent intent = new Intent(SceneryMapActivity.this,
								BNavigatorActivity.class);
						intent.putExtras(configParams);
						startActivity(intent);
					}

					@Override
					public void onJumpToDownloader() {
					}
				});
	}

}

/**
 * 继承MapView重写onTouchEvent实现泡泡处理操作
 * 
 * @author hejin
 * 
 */
class MyLocationMapView extends MapView {
	static PopupOverlay pop = null;// 弹出泡泡图层，点击图标使用

	public MyLocationMapView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public MyLocationMapView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public MyLocationMapView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (!super.onTouchEvent(event)) {
			// 消隐泡泡
			if (pop != null && event.getAction() == MotionEvent.ACTION_UP)
				pop.hidePop();
		}
		return true;
	}
}
